<?php
/**
 * Created by PhpStorm.
 * User: Yieong
 * Date: 14/10/29
 * Time: 下午8:20
 */

class OnlineController extends AdminBaseController {
	
	public function interval(){
		$this->setTitle('每日在线时长');
	
		$filter = new PubFilter('pl');
		$grid   = new PubGrid();
	
		$lod_special_id = $this->R('special_id');
		$lodos = $this->R('os');
		$filter->addSpecialFilter(false,2);
		$filter->addOsFilter($lod_special_id,true,2)
		->addServerFilter($lod_special_id,$lodos,true.false,2)
		->addFilter('time_start', 'input', PubFunc::t('时间'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-3 day'))),2)
		->addFilter('time_end', 'input', PubFunc::t('至'), array('date' => 'day', 'value' => date('Y-m-d', strtotime('-1 day'))),2);
	
		$params = $filter->get();
	
		$params = $this->indexCommon($params);
	
		$params['period'] = 'Daily';
	
	
		$header = OnlineService::intervalHeader($params);
		$data   = OnlineService::intervalData($params);
		foreach ($data as $key=>$val){
	
			$grid->addGrid($header, $val, array(
					'name'  => $key,
					'order' => array('RecDate', 'desc'),
					'fix'   => array(
							'col' => 1
					)
			));
		}
		$grid->setShowType("line");
		$this->out['filter']    = $filter->show();
		$this->out['grid']      = $grid->show();
		$this->out['is_ana'] = 2;
	}

    /**
     * 等级分布报表（暂废弃）
     */
    public function index(){
        $this->setTitle('在线时长分布报表');

        $filter = new PubFilter();
        $grid   = new PubGrid();
        $chart  = new PubChart();

        $stateOptions = array('ALL' => '在线时长分布');

        $filter->addOsFilter()
            ->addServerFilter(true,true)
            ->addFilter('time', 'timerange', PubFunc::t('日期'), array('value' => PubDateRange::getLastDayRange(7, strtotime('yesterday'))))
            ->addFilter('state', 'hidden', '', array('value' => 'ALL'))
            ->addExport();

        $params = $filter->get();

        $header = OnlineService::indexHeader();
        $data   = OnlineService::indexData($params);

        if($filter->isExport()){
            $filename = LibExcel::getFilename('在线时长分布报表', $params);
            LibExcel::exportCsv($header, $data, $filename);
        }else{
            $chartConfig  = OnlineService::indexChart($data);
            $chart->addChart('chart', $chartConfig['data'], array(
                'name'   => "在线时长人数分布",
                'type'   => 'line',
            ));
            foreach($stateOptions as $k=>$v){
                if($k == $params['state']){
                    $grid->addGrid($header, $data, array(
                        'name'  => $v,
                        'order' => array('RecDate', 'desc'),
                    ));
                }else{
                    $grid->addLink($k, $v);
                }
            }

            $this->out['filter'] = $filter->show();
            $this->out['chart'] = $chart->show();
            $this->out['grid'] = $grid->show();
        }

    }

} 